package com.wangyadong.hobby.thread;

import java.util.*;
import java.util.stream.IntStream;

public class JavaWaitingTest {

    private LinkedList<Integer> list = new LinkedList<>();
    Set set = new HashSet();

    public JavaWaitingTest() {
    }


    public synchronized void add(Integer s) throws InterruptedException {
        while (list.size() == 50) {
            wait();
        }
        this.list.add(s);
        set.add(Thread.currentThread().getName());
    }


    public synchronized Integer get() {
        while (list.size() == 0) {
            notify();
        }
        return list.getFirst();
    }

    public static void main(String[] args) throws InterruptedException {

        JavaWaitingTest waitingTest = new JavaWaitingTest();
        IntStream.range(0, 100).forEach(i -> {
            Thread thread = new Thread(() -> {
                try {
                    waitingTest.add(i);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            thread.start();
            System.out.println(thread.getState() + "  " + thread.isInterrupted());
        });


        IntStream.range(0, 50).forEach(i -> {
            new Thread(() -> {
                waitingTest.get();
            }).start();
        });

        Thread.sleep(2);

        System.out.println(waitingTest.list.size() == 100);
        System.out.println(waitingTest.set.size());
    }

}
